package com.qf.sportsevent.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.qf.sportsevent.entity.NewsBannersListHolderEntity;
import com.qf.sportsevent.entity.NewsContentsEntity;
import com.qf.sportsevent.entity.NewsContestEntity;
import com.qf.sportsevent.entity.NewsContestListEntity;
import com.qf.sportsevent.entity.NewsDetailCommentsEntity;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/16.
 */

public class CommonRecyclerAdapter<D> extends RecyclerView.Adapter<CommonRecyclerAdapter.ViewHolder>{
    private Context mContext;
    private List<D> list;
    private Map<Integer,Pair<Integer,Integer>> map;

    public CommonRecyclerAdapter(Context mContext,List<D> list, Map<Integer, Pair<Integer, Integer>> map) {
        this.mContext=mContext;
        this.list = list;
        this.map = map;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType!=0) {
            return new ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(mContext),viewType,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        D d = list.get(position);
        Pair<Integer,Integer> pair=getPair(d);
        if (pair != null) {
            holder.binding.setVariable(pair.second,d);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        D d = list.get(position);
        Pair<Integer, Integer> pair = getPair(d);
        if(pair!=null){
            return pair.first;
        }else{
            return 0;
        }
    }

    private Pair<Integer,Integer> getPair(D d){
        if(d instanceof List<?>){
            Object object = ((List) d).get(0);
            if(object instanceof NewsContestListEntity){
                return map.get(1);
            }else if(object instanceof String){
                return map.get(5);
            }else{
                return null;
            }
        }else if(d instanceof NewsBannersListHolderEntity){
            return map.get(0);
        }else if(d instanceof NewsContentsEntity){
            if(((NewsContentsEntity) d).isDetail()){
                return map.get(8);
            }else {
                return map.get(((NewsContentsEntity) d).getImage_type() + 2);
            }
        }else if(d instanceof NewsContestEntity){
            return map.get(7);
        }else if(d instanceof NewsDetailCommentsEntity){
            return map.get(10);
        }else if(d instanceof String){
            if(((String) d).contains("评论")|| d.equals("推荐阅读")) {
                return map.get(9);
            }else{
                return map.get(6);
            }
        }else{
            return null;
        }
    }

    public void add(D d){
        if(list!=null&&d!=null){
            add(list.size(),d);
        }
    }

    public void add(int position,D d){
        if(list!=null&&d!=null){
            list.add(position,d);
            notifyItemInserted(position);
        }
    }

    public void addAll(Collection<? extends D> collection){
        if(list!=null&&collection!=null){
            addAll(list.size(),collection);
        }
    }

    public void addAll(int position,Collection<? extends D> collection){
        if(list!=null&&collection!=null){
            list.addAll(position,collection);
            notifyItemRangeInserted(position,collection.size());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ViewDataBinding binding;
        public ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
